package core.controls;
import ibm.widgets.WTable;
import ibm.widgets.WTextField;

import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.util.regex.Regex;

import core.exceptions.ResultFailureException;
import core.testcases.TestObj;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class TextField extends Control<WTextField>
{
	public TextField(String propertyValue, String propertyName,
			String objClass, TestObject parent, String descriptiveName) {
		super(propertyValue, propertyName, objClass, parent, descriptiveName);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Description : Enter data in text box.
	 * 
	 * @param fieldPropValue - property value which is used for text box determination
	 * 
	 * @param fieldPropName - property name which is used for text box determination
	 * 
	 * @param textToFill - text which will be entered in text box
	 * 
	 * @param fieldType - text box type
	 * 
	 * @param parent - area in which the text box belongs.
	 */
	public boolean enter(String textToFill) throws ResultFailureException {
		// Initialize variables
		boolean result = false;
		WTextField fieldToFill = null;

		// Verify that text which will be entered is with length higher than 0
		if (textToFill.length() >= 0) {
			try {
				// Allocate needed text field
				fieldToFill = this.get();

				// Insert text
				fieldToFill.setText(textToFill);

				result = true;
			} catch (Exception e) {
				throw new ResultFailureException(
						"Unexpected assertion on entering " + textToFill
								+ " in " + this.getDescriptiveName());
			}
			finally
			{
				// Unregister object
				try
				{
					fieldToFill.unregister();
				}
				catch(NullPointerException ex)
				{
					System.out.println("The object is already unregistered!");
				}
			}
		}

		return result;
	}

	public boolean enterValue(String textToFill)
	{
        boolean result = true;
        String taskReport = "";

        try
        {
            taskReport = " \n Enter " + textToFill + " in " + this.getDescriptiveName();
            this.enter(textToFill);
        }
        catch(ResultFailureException e)
        {
        	setErrorComment("Problem appear in enetering text in " + this.getDescriptiveName(),e);
            result = false;
        }
        
        setTaskResult("Task: Enter text in " + this.getDescriptiveName() , result, taskReport);

        return result;
	}
	
	public String getText() throws ResultFailureException
	{
		String result = "";
		WTextField fieldToFill = null;

		try {
			// Allocate needed text field
			fieldToFill = this.get();

			// Get text
			result = fieldToFill.getText();

		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on getting text from " + this.getDescriptiveName());
		}
		finally
		{
			// Unregister object
			try
			{
				fieldToFill.unregister();
			}
			catch(NullPointerException ex)
			{
				System.out.println("The object is already unregistered!");
			}
		}

		return result;
	}
	
	@Override
	public WTextField get() {
		if(this.isCognosControl())
		{
			TestObject parent = this.getParent();
			this.obj = new WTextField(parent.getChildren()[0]);
		}
		else
		{
			Regex toSerach = new Regex(this.PropValue(), Regex.MATCH_MULTILINE);
			this.obj = new WTextField(toSerach,this.PropName(),this.getObjClass(),this.getParent());
		}
		return this.obj;
	}

	@Override
	public boolean isEnabled() {
		return this.get().isEnabled();
	}

	@Override
	public boolean isExist() {
		return this.get().ensureObjectIsVisible();
	}
}


